home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_315 / surf / packer.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  124 lines

  1. /*
  2.  * this file is a hacked up version of the original, who blurb is below
  3.  * changes made: internal buffer no longer static, but is now on stack
  4.  */
  5. /*----------------------------------------------------------------------*
  6.  * packer.c Convert data to "cmpByteRun1" run compression.     11/15/85
  7.  *
  8.  * By Jerry Morrison and Steve Shaw, Electronic Arts.
  9.  * This software is in the public domain.
  10.  *
  11.  *      control bytes:
  12.  *       [0..127]   : followed by n+1 bytes of data.
  13.  *       [-1..-127] : followed by byte to be repeated (-n)+1 times.
  14.  *       -128       : NOOP.
  15.  *
  16.  * This version for the Commodore-Amiga computer.
  17.  *----------------------------------------------------------------------*/
  18. #include <exec/types.h>
  19.  
  20. #define DUMP    0
  21. #define RUN     1
  22.  
  23. #define MinRun 3
  24. #define MaxRun 128
  25. #define MaxDat 128
  26.  
  27. static short putSize;
  28. #define GetByte()       (*source++)
  29. #define PutByte(c)      { *dest++ = c;   ++putSize; }
  30.  
  31. static BYTE *buf;  /* [TBD] should be 128?  on stack?*/
  32.  
  33. static BYTE *PutDump(dest, nn)
  34.     BYTE *dest;
  35.     int nn;
  36. {
  37.         int i;
  38.  
  39.     PutByte(nn-1);
  40.     for(i = 0;  i < nn;  i++)   PutByte(buf[i]);
  41.     return(dest);
  42. }
  43.  
  44. static BYTE *PutRun(dest, nn, cc)
  45.     BYTE *dest;
  46.     int nn, cc;
  47. {
  48.     PutByte(-(nn-1));
  49.     PutByte(cc);
  50.     return(dest);
  51. }
  52.  
  53. #define OutDump(nn)   dest = PutDump(dest, nn)
  54. #define OutRun(nn,cc) dest = PutRun(dest, nn, cc)
  55.  
  56. /*----------- PackRow --------------------------------------------------*/
  57. /* Given POINTERS TO POINTERS, packs one row, updating the source and
  58.    destination pointers.  RETURNs count of packed bytes.*/
  59. int PackRow(source, dest, rowSize)
  60.     BYTE *source, *dest;
  61.     int rowSize;
  62. {
  63.     char innerbuf[256];
  64.     char c,lastc = '\0';
  65.     BOOL mode = DUMP;
  66.     short nbuf = 0;             /* number of chars in buffer */
  67.     short rstart = 0;           /* buffer index current run starts */
  68.  
  69.     /*
  70.      * cute way to make local buffer known to external procedures
  71.      * since static variables increase executable size in Manx
  72.      */
  73.     buf = innerbuf; /* way to put make local buffer known to external procs */
  74.     putSize = 0;
  75.     buf[0] = lastc = c = GetByte();  /* so have valid lastc */
  76.     nbuf = 1;   rowSize--;      /* since one byte eaten.*/
  77.  
  78.  
  79.     for (;  rowSize;  --rowSize) {
  80.         buf[nbuf++] = c = GetByte();
  81.         switch (mode) {
  82.                 case DUMP:
  83.                         /* If the buffer is full, write the length byte,
  84.                            then the data */
  85.                         if (nbuf>MaxDat) {
  86.                                 OutDump(nbuf-1);
  87.                                 buf[0] = c;
  88.                                 nbuf = 1;   rstart = 0;
  89.                                 break;
  90.                                 }
  91.  
  92.                         if (c == lastc) {
  93.                             if (nbuf-rstart >= MinRun) {
  94.                                 if (rstart > 0) OutDump(rstart);
  95.                                 mode = RUN;
  96.                                 }
  97.                             else if (rstart == 0)
  98.                                 mode = RUN;     /* no dump in progress,
  99.                                 so can't lose by making these 2 a run.*/
  100.                             }
  101.                         else  rstart = nbuf-1;          /* first of run */
  102.                         break;
  103.  
  104.                 case RUN: if ( (c != lastc)|| ( nbuf-rstart > MaxRun)) {
  105.                         /* output run */
  106.                         OutRun(nbuf-1-rstart,lastc);
  107.                         buf[0] = c;
  108.                         nbuf = 1; rstart = 0;
  109.                         mode = DUMP;
  110.                         }
  111.                         break;
  112.                 }
  113.  
  114.         lastc = c;
  115.         }
  116.  
  117.     switch (mode) {
  118.         case DUMP: OutDump(nbuf); break;
  119.         case RUN: OutRun(nbuf-rstart,lastc); break;
  120.         }
  121.     return(putSize);
  122. }
  123.  
  124.